
#ifndef __STREAM_FROM_DIRECTORY_H
#define __STREAM_FROM_DIRECTORY_H

#include <vector>
#include <stack>
#include <stdint.h>
#include <apr_pools.h>
#include <apr_thread_proc.h>
#include "../db/NewKeyDB.h"

/** Serialises the contents of a directory. */
class StreamFromDirectory
{
   public:
      /** Stream section flag. */
      enum currflagtype {   WRITING_FILE_NAME_SIZE   = 0,
                            WRITING_FILE_STREAM_SIZE = 1,
                            WRITING_NAME             = 2,
                            WRITING_STREAM           = 3   };
      StreamFromDirectory(const char *directorypath, apr_pool_t *imp);
      ~StreamFromDirectory();
      void getNextBlock(char *blockbuffer, apr_off_t *tsize);
      char *GetNextBlockEncrypted(const char *cname, KeyPair *kp, int *realsize, int *cesize, char **hash);
      char *GetNextBlockEncrypted(const char *encwith, KeyDB *kdb, int *realsize, int *cesize, char **hash);
      char *getLocalPathToThisFile();
      char *getPathToThisFile();
      static void writeLogical64Bit(int64_t towrite, unsigned char *bytes);
      int writeDataOnFile(char *dst, apr_off_t *maxlen);
   private:
      /** APR memory pool to use for this instance. */
      apr_pool_t *mp;
      /** Directory that is to be serialised. */
      char *directory_name;
      /** A stack of directory pointers. */
      std::stack<apr_dir_t *> currdirpntrs;
      /** A stack of directory names. */
      std::vector<char *> dirnames;
      /** The file info structure for the file currently under consideration. */
      apr_finfo_t *currfinfopntr;
      /** A flag indicating the current section of the stream under construction. */
      int currflag;
      /** The number of bytes of the current section of the stream that have
       * already been written. */
      int64_t currbytesthrough;
};

#endif
